package com.tao.utils;

import com.tao.pojo.vo.LoginUser;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;

@Component
public class GlobalUtils {

    @Autowired
    private  HttpServletRequest request;

    @Autowired
    private  RedisTemplate redisTemplate;

    public  LoginUser getLoginUser(){
        String token = request.getHeader("Authorization");
        String userid;
        try {
            Claims claims = JwtUtil.parseJWT(token);
            userid = claims.getSubject();
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("token非法");
        }
        //从redis中获取用户信息
        return (LoginUser) redisTemplate.opsForValue().get("login:" + userid);
    }
}
